﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class ParticipationDa
    {
        public static void InsertParticipation(Participation partic)
        {
            InsertParticipation(partic, DBHelper.GetDatabase(), null);
        }
        public static void InsertParticipation(Participation partic, TranscationHelper trans)
        {
            InsertParticipation(partic, trans.DataBase, trans.Transaction);
        }
        private static void InsertParticipation(Participation partic, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                partic.ExternalUserId,
                partic.SurveyId,
                partic.IsValid,
                partic.JoinTime
            };

            try
            {
                if (trans == null)
                    partic.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertParticipation", parameters));
                else
                    partic.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertParticipation", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeletetParticipation(int id)
        {
            Database db = DBHelper.GetDatabase();

            try
            {
                db.ExecuteNonQuery("Usp_DeleteParticipation", id);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpateParticipation(Participation partic)
        {
            UpateParticipation(partic, DBHelper.GetDatabase(), null);
        }
        public static void UpateParticipation(Participation partic, TranscationHelper trans)
        {
            UpateParticipation(partic, trans.DataBase, trans.Transaction);
        }
        private static void UpateParticipation(Participation partic, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                partic.Id,
                partic.ExternalUserId,
                partic.SurveyId,
                partic.IsValid,
                partic.JoinTime
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateParticipation", parameters);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateParticipation", parameters);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static Participation GetParticipation(int id)
        {
            Database db = DBHelper.GetDatabase();
            Participation partic = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetParticipation", id))
            {
                if (dataReader.Read())
                {
                    partic = PopulateParticipation(dataReader);
                }
            }
            return partic;
        }

        public static List<Participation> GetParticipationList(int? extUserId, int? surveyId)
        {
            Database db = DBHelper.GetDatabase();
            List<Participation> partics = new List<Participation>();

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetParticipationList", extUserId, surveyId))
            {
                while (dataReader.Read())
                {
                    partics.Add(PopulateParticipation(dataReader));
                }
            }
            return partics;
        }

        private static Participation PopulateParticipation(IDataReader dataReader)
        {
            Participation partic = new Participation();
            partic.Id = dataReader.GetInt32(0);
            partic.ExternalUserId = dataReader.GetInt32(1);
            partic.SurveyId = dataReader.GetInt32(2);
            if (!dataReader.IsDBNull(3))
                partic.IsValid = dataReader.GetBoolean(3);
            partic.JoinTime = dataReader.GetDateTime(4);

            return partic;
        }
    }
}
